    //
//  BlogViewController.m
//  VICKY ZONE
//
//  Created by sunx on 15/3/21.
//  Copyright (c) 2015年 sunx. All rights reserved.
//

#define SERVE @"http://samoyun.com/controller/blogController.php"
#import "BlogViewController.h"
#import "SVProgressHUD.h"
#import "NSURLSession+KKAddition.h"
#import "EditViewController.h"
#import "GDataXMLNode.h"
#import "BlogCell.h"
#import "AxisCell.h"

@interface BlogViewController ()
@property(nonatomic,strong) NSArray *blogs;
@property(nonatomic,strong) NSString *username;
@end

@implementation BlogViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //NSLog(@"%@",NSHomeDirectory());
    self.tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(jumpToEdit)];
    self.navigationItem.rightBarButtonItem = barButton;
    
    self.view.backgroundColor = [UIColor colorWithRed:56.0/255.0 green:56.0/255.0 blue:56.0/255 alpha:0.9];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    self.username = [userDefault valueForKey:@"username"];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [SVProgressHUD show];
    
    [NSURLSession GET:SERVE pararms:@{@"user":self.username,@"type":@"blog"} completed:^(NSData *data) {
        NSError *error;
        GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
        NSArray *arr = [[[document rootElement] children][2] children];
        self.blogs = arr;
        //NSLog(@"%d",(int)[arr count]);
        [self performSelectorOnMainThread:@selector(updateTable) withObject:self waitUntilDone:YES];
        
    } failed:^(NSError *error) {
        if(!error) {
            NSLog(@"%@",error);
        }
    }];
}
- (void)jumpToEdit {
    EditViewController *viewController = [[EditViewController alloc] initWithType:@"blog"];
    [self.navigationController pushViewController:viewController animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)updateTable {
    NSMutableArray *cells = [NSMutableArray new];
    
    for(int i = 0; i < [self.blogs count]; i++) {
        NSString *title = [[self.blogs[i] children][1] stringValue];
        NSString *body = [[self.blogs[i] children][2] stringValue];
        NSString *date = [[self.blogs[i] children][3] stringValue];
        NSArray *imags = [[self.blogs[i] children][4] children];
        NSMutableArray *imageURLs = [NSMutableArray new];
        for(int j = 0; j < [imags count];j ++) {
            [imageURLs addObject:[imags[j] stringValue]];
        }
        
        
        BlogCell *cell = [[BlogCell alloc] initWithTitle:title andBody:body andDate:date andImages:imageURLs];
        [cells addObject:cell];
    }
    
    OrganicSection *sec = [OrganicSection sectionWithCells:cells];
    
    self.sections = @[sec];
    
    [self.tableView reloadData];
    
    [SVProgressHUD dismiss];
}

@end
